package com.tc168.cp306.b06.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;


import com.tc168.cp306.b06.MyApplication;
import com.tc168.cp306.b06.Utils.SPUtils;
import com.tc168.cp306.b06.gen.UserDao;

import java.util.HashMap;
import java.util.Map;


//todo 基类
public abstract class BaseActivity extends AppCompatActivity {

    private final long RETRY_TIMES = 2;
    private boolean showLoading = true;
    public ProgressDialog pd;

    private SharedPreferences sp;
    public boolean isDeng;

    public String spname;

    private Map map = new HashMap();

    protected static final int PRC_PHOTO_PICKER = 1;

    protected static final int RC_CHOOSE_PHOTO = 1;
    protected static final int RC_PHOTO_PREVIEW = 2;
    public UserDao personbeanDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getIntentData(savedInstanceState);
        initapp();
        loadXml();
        initView();
        initData();
        registerListener();
        personbeanDao = MyApplication.getmDaoSession().getUserDao();

    }


    public void initapp() {

        isDeng = (Boolean) SPUtils.get(BaseActivity.this, "isdeng", false);
        spname = (String) SPUtils.get(BaseActivity.this, "name", "");
    }

    //获取系统时间戳
    public String getTime() {
        long time = System.currentTimeMillis() / 1000;//获取系统时间的10位的时间戳
        String str = String.valueOf(time);

        return str;

    }

    @Override
    protected void onResume() {
        super.onResume();

        //页面进入
        map.clear();
        map.put("content", this.getClass().getSimpleName());
    }

    @Override
    protected void onStop() {
        super.onStop();

        if (pd != null && pd.isShowing()) {
            pd.dismiss();
        }

        //页面退出
        map.clear();
        map.put("content", this.getClass().getSimpleName());
    }

    public void setLoadingFlag(boolean show) {
        showLoading = show;
    }

    /**
     * 得到上一个Activity传来的Intent数据
     *
     * @param savedInstanceState
     */
    protected abstract void getIntentData(Bundle savedInstanceState);

    /**
     * 加载布局
     */
    protected abstract void loadXml();

    /**
     * 初始化控件
     */
    protected abstract void initView();

    /**
     * 初始化数据
     */
    protected abstract void initData();

    /**
     * 设置监听
     */
    protected abstract void registerListener();


    //TODO：隐藏软键盘的操作
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_DOWN &&
                getCurrentFocus() != null &&
                getCurrentFocus().getWindowToken() != null) {

            View v = getCurrentFocus();
            if (isShouldHideKeyboard(v, event)) {
                hideKeyboard(v.getWindowToken());
            }
        }
        return super.dispatchTouchEvent(event);
    }

    /**
     * 根据EditText所在坐标和用户点击的坐标相对比，来判断是否隐藏键盘，因为当用户点击EditText时则不能隐藏
     */
    private boolean isShouldHideKeyboard(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] l = {0, 0};
            v.getLocationOnScreen(l);
            int left = l[0],
                    top = l[1],
                    bottom = top + v.getHeight(),
                    right = left + v.getWidth();
            if (event.getRawX() > left && event.getRawX() < right
                    && event.getRawY() > top && event.getRawY() < bottom) {
                // 点击EditText的事件，忽略它。
                return false;
            } else {
                return true;
            }
        }
        // 如果焦点不是EditText则忽略，这个发生在视图刚绘制完，第一个焦点不在EditText上，和用户用轨迹球选择其他的焦点
        return false;
    }

    /**
     * 获取InputMethodManager，隐藏软键盘
     */
    private void hideKeyboard(IBinder token) {
        if (token != null) {
            InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
            mInputMethodManager.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }


    public void goActivity(Context _this, Class<? extends Activity> _class) {
        Intent intent = new Intent(_this, _class);
        startActivity(intent);
    }

    public void HideKeyBord() {
        //隐藏软件盘
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
    }

    //判断登录状态
    public boolean getLoginStatus() {
        sp = this.getSharedPreferences("Userinfo", Context.MODE_PRIVATE);
        if (sp.getBoolean("STATUS", false)) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


}
